/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.edu.amu.wmi.kino.visualjavafx.nodes.factory;

import org.openide.nodes.Node;
import org.openide.util.Lookup;


/**
 * this class is used for creating nodes for model objects - you can implement
 * your own NodeCreator classes for some specific objects if you want to. 
 * Usage: Node n = NodeFactory.generateFrom(obj)
 * @author psychollek
 */
public class NodeFactory {

    private NodeFactory(){};

    private static Lookup.Result<NodeCreator> creators = 
            Lookup.getDefault().lookupResult(NodeCreator.class);

    /**
     * This Method Asks the lookup for NodeCreator object that can create the node
     * for suplied object, for the lookup to find one, the creator's calss have
     * to be exported through entering it's full class name in file in its jar
     * META-INF/services/pl.edu.amu.wmi.kino.visualjavafx.nodes.factory.NodeCreator .
     * Shall the method fail to find creator - it will return null.
     * @param obj the object to be used for creating node.
     * @return Node representing object, or null.
     */
    public static Node generateFrom(Object obj) {


        for(NodeCreator c: creators.allInstances()){

            if( c.canCreateNodeFrom(obj) ){
                return c.generateNode(obj);
            }
        }

        
        return null;
    }

}
